package drds.server.net.handler;

import drds.server.config.ErrorCode;
import drds.server.net.Handler;
import drds.server.net.packet.Packet;
import drds.server.server.SessionContext;

/**
 * 前端命令处理器
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class CommandHandler implements Handler {

	protected final SessionContext sessionContext;

	public CommandHandler(SessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	@Override
	public void handle(byte[] data) {
		switch (data[4]) {
		case Packet.QUERY:
			sessionContext.query(data);
			break;
		case Packet.PING:
			sessionContext.ping();
			break;
		case Packet.QUIT:
			sessionContext.close("quit cmd");
			break;
		case Packet.KILL_PROCESS:
			sessionContext.kill(data);
			break;
		case Packet.STATEMENT_PREPARE:
			sessionContext.statementPrepare(data);
			break;
		case Packet.COM_STMT_SEND_LONG_DATA:
			break;
		case Packet.STATEMENT_RESET:
			break;
		case Packet.STATEMENT_EXECUTE:
			break;
		case Packet.STATEMENT_CLOSE:
			break;
		case Packet.HEARTBEAT:
			sessionContext.heartbeat(data);
			break;
		default:
			sessionContext.writeErrorMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Unknown command");

		}
	}

}